module.exports = function($scope, $state, $stateParams, $modalInstance, $modal, $resource,FileUploader,toaster, item, type, flag){
  console.log(item);
  $scope.info = {};
  $scope.obj = {
      check_product_satisfy : '0',
      check_product_reduce : '0',
      check_product_satisfys : '0',
      check_product_reduces : '0' , 
  }
  $scope.searchform = {};
  $scope.vm = {
      'date' : '',
      'options' : {
          format: "YYYY-MM-DD",
          locale : 'zh-cn'
      }
  }
  $scope.type = type;
  $scope.flag = flag;
  $scope.info = item;
  $scope.info.check_type = '0';
  console.log($scope.info);
  if($scope.info.list != undefined && $scope.info.list.length == 2){
      $scope.obj.check_product_satisfy = $scope.info.list[0].check_product_satisfy;
      $scope.obj.check_product_reduce = $scope.info.list[0].check_product_reduce;
      $scope.obj.check_product_satisfys = $scope.info.list[1].check_product_satisfy;
      $scope.obj.check_product_reduces = $scope.info.list[1].check_product_reduce;
      if ($scope.info.list[1].card_type == '1'){
          $scope.info.card_type = '1';
      }
  } else if($scope.info.list != undefined && $scope.info.list.length == 1){
      $scope.obj.check_product_satisfy = $scope.info.list[0].check_product_satisfy;
      $scope.obj.check_product_reduce = $scope.info.list[0].check_product_reduce;
      $scope.info.card_type = '0';
  }
  console.log('详情');
  console.log($scope.obj);
  if($scope.info.check_product_img != undefined){
    $scope.array = $scope.info.check_product_img.split(',');
  }
  // 商家名称
  $resource('/api/as/mc/shop/findShopLeagueList', {}, {}).
  save({},function(res) {
      if (res.errcode !== 0) {
          toaster.error({title:"",body:res.errmsg});
          return;
      }
      $scope.shopArr = res.data;
      if ($scope.info.place_name != undefined){
        for(var i = 0; i < $scope.shopArr.length; i++){
          if ($scope.info.place_name == $scope.shopArr[i].name){
              $scope.searchform.place = $scope.shopArr[i];
          }
        }
      }
  })
  $scope.ok = function(){
      $scope.list = [];
      console.log($scope.searchform);
      $scope.info.place_id = $scope.searchform.place.code;
      $scope.info.place_name = $scope.searchform.place.name;
      console.log($scope.obj.check_product_satisfy, $scope.obj.check_product_reduce, $scope.obj.check_product_satisfys, $scope.obj.check_product_reduces);
      if ($scope.obj.check_product_satisfy == '' || $scope.obj.check_product_reduce == '') {
          toaster.warning({title:"",body:'普通折扣不能为空'});
      } else if ($scope.obj.check_product_satisfy == '0' && $scope.obj.check_product_reduce == '0') {
          toaster.warning({title:"",body:'普通折扣不能为0'});
      } else {
          $scope.list1 = {
            'card_type' : '0', 
            'check_product_satisfy' : $scope.obj.check_product_satisfy,
            'check_product_reduce' :  $scope.obj.check_product_reduce
          }
          $scope.list.push($scope.list1);
          console.log($scope.list);
      }
      
      if ($scope.obj.check_product_satisfys == '' || $scope.obj.check_product_reduces == ''){
          alert('df');
          toaster.warning({title:"",body:'VIP折扣不能为空'});
      } else if ($scope.obj.check_product_satisfys == '0' && $scope.obj.check_product_reduces == '0'){
          toaster.warning({title:"",body:'VIP折扣不能为0'});
      } else {
          $scope.list2 = {
            'card_type' : '1', 
            'check_product_satisfy' : $scope.obj.check_product_satisfys,
            'check_product_reduce' :  $scope.obj.check_product_reduces
          }
          $scope.list.push($scope.list2);
          $scope.info.list = $scope.list;
          console.log($scope.list);
          $resource('/api/ac/cdc/leagueCardCheckProductService/insertCardCheckProduct', {}, {}).
          save($scope.info,function(res) {
              if (res.errcode !== 0) {
                  toaster.error({title:"",body:res.errmsg});
                  return;
              }
              console.log(res);
              toaster.success({title:"",body:"操作成功"});
              $modalInstance.close();
          })
      }
      
  }
  $scope.cancel = function(){
    $modalInstance.close();
  }
  $scope.image = function(){
    var para = $state.get('app.imageupload');
    //设置上传文件夹，以自己业务命名
    angular.extend(para, {
      resolve : {  
        'dir' : function(){
          return 't1';
        }
      }  
    })
    var modalInstance = $modal.open(para);
    modalInstance.opened.then(function() {// 模态窗口打开之后执行的函数  
      console.log('modal is opened');  
    });  
    modalInstance.result.then(function(result) {  
      console.log(result);  
      // $scope.imagearr = JSON.stringify(result);
      $scope.array = result;
      $scope.info.check_product_img = $scope.array.join(',');
      console.log($scope.array.join(',')
    );
    }, function(reason) {  
      console.log(reason);// 点击空白区域，总会输出backdrop  
      // click，点击取消，则会暑促cancel  
      $log.info('Modal dismissed at: ' + new Date());  
    }); 
  };
  $scope.del=function(index){
      $scope.array.splice(index,1);
      console.log($scope.array);
      $scope.info.check_product_img=$scope.array.join(',');
      console.log($scope.info.check_product_img);
  }
  

};